﻿// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <curl/curl.h>
#include <map>
#include <regex>
#include <filesystem>
using namespace std;
const static string URL = "http://127.0.0.1";
vector<map<string, string>> VT_IMAGE = vector<map<string, string>>();
const static string PATH = "./images";

size_t writeCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
    size_t totalSize = size * nmemb;
    output->append((char*)contents, totalSize);
    return totalSize;
}

string _Request_GET(const string& url, map<string, string> headers) {
    CURL* curl;
    CURLcode statusCode;
    string htmlData;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if (curl) {
        // 设置要抓取的网页地址
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
        //"PHPSESSID=8iliip6qarl0a938la306di61m"
        if (headers.find("cookies") != headers.end()) {
            curl_easy_setopt(curl, CURLOPT_COOKIE, headers["cookies"].c_str());
        }
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        // 设置回调函数，用于处理获取到的响应数据
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlData);
        // 执行 HTTP 请求
        auto res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        }

        // 输出获取到的网页内容
        //std::cout << htmlData << std::endl;

        // 清理 CURL 实例
        curl_easy_cleanup(curl);
        return htmlData;
    }
    else {
        std::cerr << "curl_easy_init() failed." << std::endl;
        return "";
    }
    // 清理 libcurl
    curl_global_cleanup();
}

bool _In_Vector(const string& value, vector<string>& vt) {
    return find(vt.begin(), vt.end(), value) != vt.end();
}

void _Init(const string& html) {
    //./img/movie10.png
    regex reg("/img/(\\w+\\.)(jpg|png)");
    sregex_iterator it(html.begin(), html.end(), reg);
    auto end_it = sregex_iterator();
    vector<string> v0;
    string url, image_name;
    map<string, string> ResultMap;
    while (it != end_it) {
        if (_In_Vector(it->str(), v0)) {
            ++it;
            continue;
        }
        v0.push_back(it->str());
        url = it->str(); 
        image_name = (*it)[1].str() + (*it)[2].str();
        ResultMap["url"] = url;
        ResultMap["image_name"] = image_name;
        VT_IMAGE.push_back(ResultMap);
        //cout << it->str() << endl;
        ++it;
    }
}

size_t write_callback(void* ptr, size_t size, size_t nmemb, void* stream) {
    return fwrite(ptr, size, nmemb, (FILE*)stream);
}

int download_file(const char* url, const char* output_filename) {
    CURL* curl = curl_easy_init();
    FILE* fp = fopen(output_filename, "wb");  // 打开一个文件用于保存下载的数据

    if (curl && fp) {
        // 设置下载选项
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);

        // 执行下载请求
        CURLcode res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            fprintf(stderr, "下载失败: %s\n", curl_easy_strerror(res));
            fclose(fp);
            curl_easy_cleanup(curl);
            return -1;
        }

        fclose(fp);
    }
    else {
        fprintf(stderr, "初始化失败\n");
        if (fp) {
            fclose(fp);
        }
        if (curl) {
            curl_easy_cleanup(curl);
        }
        return -1;
    }

    curl_easy_cleanup(curl);
    return 0;
}

int main()
{
    //SetConsoleOutputCP(65001);
    if (!filesystem::exists(PATH)) {
        filesystem::create_directory(PATH);
    }
    map<string, string> headers;
    headers["cookies"] = "PHPSESSID=8iliip6qarl0a938la306di61m";
    string html;
    html = _Request_GET(URL, headers);
    //cout << html << endl;
    _Init(html);
    string image_url, image_name;
    map<string, string> resultMap;
    while (!VT_IMAGE.empty()) {
        resultMap = VT_IMAGE.back();
        VT_IMAGE.pop_back();
        image_url = URL + resultMap["url"];
        image_name = PATH + "/" + resultMap["image_name"];
        const char* url = image_url.c_str();
        const char* file_name = image_name.c_str();
        download_file(url, file_name);
    }

}

